<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- card区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <!-- 搜索区域 搜索框 -->
          <el-input
            v-model="queryInfo.query"
            placeholder="请输入内容"
            class="input-with-select"
            clearable
            @clear="getGoodslist"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodslist"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddGoodsLine">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="750px"></el-table-column>
        <el-table-column align="center" prop="goods_price" label="商品价格(元)"></el-table-column>
        <el-table-column align="center" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column align="center" prop="add_time" label="创建时间"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain icon="el-icon-edit"></el-button>
            <el-button
              size="mini"
              type="danger"
              plain
              icon="el-icon-delete"
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能  -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 定义一个商品列表的请求参数数组
      queryInfo: {
        query: '', // 查询参数
        pagenum: 1, // 显示当前页
        pagesize: 10 // 每页数据条数
      },

      // 接收数据列表数组
      goodsList: [],

      // 数据总数
      total: 0
    }
  },
  created () {
    this.getGoodslist()
  },
  methods: {
    // 获取商品列表数据方法
    async getGoodslist () {
      const { data: res } = await this.$http.get('/goods/', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // this.$message.success(res.meta.msg)
      // console.log(res.data)
      res.data.goods.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD hh:mm')
      })
      this.goodsList = res.data.goods
      this.total = res.data.total
    },

    // 当前页码条数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodslist()
    },

    // 当前显示页码
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodslist()
    },

    // 点击删除按钮删除商品
    async removeGoods (id) {
      const confirmResult = await this.$confirm('是否将商品放入回收站, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') return this.$message.info('您取消了删除')
      const { data: res } = await this.$http.delete(`/goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getGoodslist()
    },

    // 编程式导航
    goAddGoodsLine () {
      this.$router.push('/goods/addgood')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
